/**
 * 消息事件总线（设计模式 == 消息发布订阅）
 * typescript 版本
 */
class GxEventBus {
  listeners: Map<string, Function> | null;
  constructor(listeners?: Map<string, Function>) {
    this.listeners = listeners || null;
  }
  public addListener(event: string, func: Function) {
    if (!this.listeners) {
      this.listeners = new Map<string, Function>();
    }
    this.listeners.set(event, func);
  }
  public publish(event: string, options: any) {
    if (!this.listeners){
        return;
    }
    if (this.listeners.has(event)) {
      const func = this.listeners.get(event)
      if(func){
        func(options)
      }
    }
  }
  public removeListener(evnent: string) {
    if (this.listeners && this.listeners.has(evnent)) {
      this.listeners.delete(evnent);
    }
  }
}
const eventBus = new GxEventBus();
export default eventBus;
